#include <windows.h>
#include "resource.h"
#include "resource1.h"
#include "resource2.h"
#include "resource3.h"
#include "resource4.h"

const char g_szClassName[] = "myWindowClass";
// Step 6: the Toolbar Dialog Procedure
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_COMMAND:
			switch(LOWORD(wParam))
				{
				case IDOK1:
					MessageBox(hwnd, "You have selected <OK>. Program will shut down.", "",
					MB_OK | MB_ICONEXCLAMATION);
					EndDialog(hwnd, IDOK1);
					break;
				case IDCANCEL1:
					MessageBox(hwnd, "You have selected <cancel>.Program will shut down.", "",
					MB_OK | MB_ICONEXCLAMATION);
					EndDialog(hwnd, IDCANCEL1);
					break;
				}
		break;
		default: return FALSE;
	}
return TRUE;
}
// Step 5: the Dialog Box Procedure
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){
	switch(Message){
		case WM_INITDIALOG:
			return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wParam)){
			case IDOK:
				EndDialog(hwnd, IDOK);
				break;
			case IDCANCEL:
				EndDialog(hwnd, IDCANCEL);
				break;
			}
			break;
			default:	return FALSE;
	}
	return TRUE;
}
// Step 4: the Window Procedurea
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_EXIT:
				PostMessage(hwnd, WM_CLOSE, 0, 0);
				break;
				case ID_HELP_DEMONSTRATOR:
				int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AboutDlgProc);
				if(ret == IDOK){
					MessageBox(hwnd, "Dialog exited with IDOK.", "Notice",
					MB_OK | MB_ICONINFORMATION);
				}
					else if(ret == IDCANCEL){
						MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice",
						MB_OK | MB_ICONINFORMATION);
					}
						else if(ret == -1){
							MessageBox(hwnd, "Dialog failed!", "Error",
							MB_OK | MB_ICONINFORMATION);
			}
				break;
			}
		case WM_LBUTTONDOWN:{
				HWND g_hToolbar = NULL;
				g_hToolbar = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),hwnd, ToolDlgProc);
				if(g_hToolbar != NULL)
				{
					ShowWindow(g_hToolbar, SW_SHOW);
				}
				else
					{
						MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",
						MB_OK | MB_ICONINFORMATION);
					}
			break;}
		case WM_RBUTTONDOWN:{
			char szFileName[MAX_PATH];
			HINSTANCE hInstance = GetModuleHandle(NULL); 
			GetModuleFileName(hInstance, szFileName, MAX_PATH);
			MessageBox(hwnd, szFileName, "You can find this program in:", MB_OK | MB_ICONINFORMATION);
			break;}
		case WM_MBUTTONDOWN:{
			char szFileName[MAX_PATH];
			HINSTANCE hInstance = GetModuleHandle(NULL); 
			GetModuleFileName(hInstance, szFileName, MAX_PATH);
			MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
			break;}
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON2));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON2),IMAGE_ICON, 16, 16, 0);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Demonstrator Grafic Arbori B",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 1000, 650,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
		/*WNDPROC fWndProc = (WNDPROC)GetWindowLong(Msg.hwnd, GWL_WNDPROC);
		fWndProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);*/
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;

}